package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNPicList;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.UIHelper;

import java.util.List;
import java.util.Random;

public class TimeLineGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater inflater;
    private List<YNPicList> mList;
    private int nums;
    private RequestManager glide;

    public TimeLineGridViewAdapter(Context mContext, List<YNPicList> mItems, int nums) {
        super();
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
        glide = Glide.with(mContext);
        if (nums <= 6) {
            this.mList = mItems;
        } else if (nums > 6) {
            this.mList = mItems.subList(0, 6);
        }
        this.nums = nums;
    }


    @Override
    public int getCount() {
        if (mList == null) {
            return 0;
        } else {
            return mList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mList == null) {
            return null;
        } else {
            return this.mList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.timeline_gridview_item, null, false);
            holder.iv = (ImageView) convertView.findViewById(R.id.gridview_item_iv);
            holder.num = (TextView) convertView.findViewById(R.id.gridview_item_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.iv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, UIHelper.getTmThreeGdHigh(mContext)));
        if (nums > 6 && position == 5) {
            holder.num.setText(String.valueOf(nums));
            holder.num.setVisibility(View.VISIBLE);
        } else {
            holder.num.setVisibility(View.GONE);
        }
//        ImageOptHelper.showFrescoStatus(Uri.parse(path), holder.iv, mContext);
//        ImageOptHelper.showFrescoStatus(StringUtils.repaceImgUrl(YNUrlHelper.getStatusUrl(mContext, mList.get(position).getThumbnail())), , mContext);
        glide.load(mList.get(position).getThumbnail())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(ImageOptHelper.listimg[new Random().nextInt(12)])
                .into(holder.iv);
        return convertView;
    }

    private class ViewHolder {
        ImageView iv;
        TextView num;
    }


}

